package com.family.thirdpart.crawler;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.family.thirdpart.crawler.to.HousingTo;
import com.family.thirdpart.feign.HorseFeignClient;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.concurrent.locks.ReentrantLock;

@Service
@Log4j2
public class HorseCrawler {
    private static final String Base_URL="https://sh.lianjia.com/ershoufang/{page}";
    @Autowired
    private HorseFeignClient feignClient;

    public Integer start(){
        try (Entry entry=SphU.entry("HorseCrawler")){
            String baseUrl="";
            //写死100页的内容
            HousingTo housingTo = new HousingTo();
            for (int i = 0; i < 1; i++) {
                if(i==0){
                    baseUrl=Base_URL.replace("{page}","");
                }else {
                    baseUrl=StringUtils.replace(Base_URL, "{page}", "pg2"+i);
                }
                String s = doGet(baseUrl);
                Document parse = Jsoup.parse(s);
                Elements select = parse.select(".sellListContent li");
                Integer num=0;
                if(select.size()>0&&select!=null){
                    for (Element element : select) {
                        if(element.hasText()){
                            String id = element.attr("data-lj_action_housedel_id");//获取id
                            String title = element.select(".title a").text();//获取标题
                            String flood = element.select(".flood a").text();//获取地址
                            String totalPrice = element.select(".priceInfo .totalPrice.totalPrice2 span").text();//获取售价
                            String unitPrice = element.select(".priceInfo .unitPrice span").text();//获取单价
                            String attr = element.select(".houseInfo").text();//获取单价
                            String img = element.select(".lj-lazy").attr("data-original");//获取图片地址
                            housingTo.setHousingName(title);
                            housingTo.setBh(id);
                            housingTo.setHousingName(title);
                            BigDecimal bigDecimal = new BigDecimal(totalPrice);
                            housingTo.setSelling(bigDecimal);
                            housingTo.setHousingTypes(1);//二手房
                            housingTo.setHousetype(flood);
                            if(attr!=null&&!attr.equals("")){
                                String[] split = attr.split("\\|");
                                if(split.length>=0){
                                    housingTo.setHousetype(split[0]);
                                    housingTo.setAcreage(split[1]);
                                    housingTo.setOrientation(split[2]);
                                    housingTo.setFitment(split[3]);
                                    housingTo.setFloor(split[4]);
                                    housingTo.setDecade(split[5]);
                                }
                            }
                            housingTo.setHousesId(1);
                            housingTo.setImg(img);
                            housingTo.setCrawlerNum(num++);
                            housingTo.setUpdateDay(new Date());
                            feignClient.save(housingTo);
                        }
                    }
                }
            }
            return housingTo.getCrawlerNum();
        }catch(BlockException blockException){
            log.error("资源被限流", blockException.getMessage());
        }
        return null;
    }
    public String doGet(String url) {
        // 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // 创建Get请求
        HttpGet httpGet = new HttpGet(url);
        // 响应模型
        CloseableHttpResponse response = null;
        try {
            // 由客户端执行(发送)Get请求
            response = httpClient.execute(httpGet);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
                String s = EntityUtils.toString(responseEntity, "UTF-8");
                return s;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

}
